using System;
using System.Collections.Generic;

using log4net;

using GymManager.Exception;
using GymManager.Mechanism;
using GymManager.Persistence.DAO;
using GymManager.Persistence.VO;

namespace GymManager.Business
{
    /// <summary>
    /// Class responsible by providing services regarding Medical Exam
    /// </summary>
    public class MedicalExamServices
    {
        // logger instance
        private ILog _logManager = LogManager.GetLogger(typeof(MedicalExamServices));

        /// <summary>
        /// Method responsible by getting all medical exams beloging to an specific student
        /// </summary>
        /// <param name="Int32">student identifier</param>
        /// <returns>IList - list of medical exams belonging to an specific student</returns>
        /// <see cref="GymManager.Exception.BusinessException"/>
        public IList<MedicalExam> findByStudent(Int32 studentId)
        {
            if (_logManager.IsInfoEnabled)
            {
                _logManager.Info("Begin Method");
            }

            IList<MedicalExam> medicalExamList = null;

            try
            {
                // getting all medical exams belonging to an specific user
                MedicalExamDAO medicalExamDAO = new MedicalExamDAO();
                medicalExamList = medicalExamDAO.findByStudent(studentId);
            }
            catch (PersistenceException)
            {
                throw new BusinessException();
            }            
            
            if (_logManager.IsInfoEnabled)
            {
                _logManager.Info("End Method");
            }

            return medicalExamList;
        }

        /// <summary>
        /// This method is responsible by generating medical exams entries to each active student
        /// in database. A medical exam must be done every year, therefore, every active student must
        /// have an entry (done or not) for the current year.
        /// </summary>
        /// <see cref="GymManager.Exception.BusinessException"/>
        public void generatePendingMedicalExams()
        {
            try
            {
                // retrieve all active students
                StudentDAO studentDAO = new StudentDAO();
                IList<Student> studentList = studentDAO.findAllActive();

                if (studentList != null && studentList.Count > 0)
                {
                    // common structures
                    MedicalExamDAO medicalExamDAO = new MedicalExamDAO();
                    MedicalExam mostRecentMedicalExamVO = null;
                    MedicalExam medicalExamVO = null;

                    // processing each student information
                    foreach(Student studentVO in studentList)
                    {
                        // getting the most recent medical exam for the current student
                        mostRecentMedicalExamVO = medicalExamDAO.findMostRecentByStudent(studentVO.Id);

                        // check if the medical has been already done this year
                        if (mostRecentMedicalExamVO == null || mostRecentMedicalExamVO.ExamDate.Year < DateTime.Now.Year)
                        {
                            // creating a new medical exam for the current user
                            medicalExamVO = new MedicalExam();
                            medicalExamVO.StudentId = studentVO.Id;
                            medicalExamVO.ExamDate = DateTime.Now.AddDays(10);

                            // adding medical exam
                            medicalExamDAO.create(medicalExamVO);
                        }
                    }
                }
            }
            catch (PersistenceException)
            {
                throw new BusinessException();
            }
        }
     }
}
